<?php

/**
 * Options Framework Handle front end function
 * 
 */

add_filter('body_class', 'apollo_body_class');
    
/**
 * Filter the body_class
 *
 * Throwing different body class for the different layouts in the body tag
 */
function apollo_body_class( $classes ) {
    
    $default_layout = of_get_option( 'apollo_default_layout', 'right_sidebar_one_column' );
   
    if ( $default_layout == 'right_sidebar_two_columns' ) {
        $classes[] = 'grid';
    }
    
    return $classes;
}

class apollo_default_template {
    
    function init() {
        
        // Handle after activate theme to set default template
        add_action('after_switch_theme', array( $this, 'apollo_update_default_template' ));
    }
    
    /**
    * Add default widgets
    * These functions are meant to update options that contain header, footer, siteinfo.
   /**
    * @return void
    */
    function add_widgets() {
        
        $sidebar_options = get_option('sidebars_widgets');
        $sidebar_options['sidebar-primary'] = array ();
        update_option('sidebars_widgets', $sidebar_options);
        
        $this->add_widget( 'apollo_search_widget' );
        $this->add_widget( 'apollo_custom_widget', 'sidebar-primary', array( 
            'title'     => '',
            'content'   => '<a href="#" target="_blank"><img src="/wp-content/themes/apollo/assets/uploads/sgl.jpg" alt="sale"></a>'
        ) );
        $this->add_widget( 'apollo_most_reviews_widget' );
        $this->add_widget( 'apollo_subscribe_widget' );
        $this->add_widget( 'apollo_custom_widget', 'sidebar-primary', array( 
            'title'     => 'GET YOUR FREE',
            'content'   => '<a href="#" target="_blank"><img src="/wp-content/themes/apollo/assets/uploads/sale.jpg" alt="sale"></a>'
        ) );
    }
    
    function add_widget( $widget_name = 'apollo_custom_widget', $add_to_sidebar = 'sidebar-primary', $settings = array() ) {
        
        // Reset empty sidebar-primary
        $sidebar_options = get_option('sidebars_widgets');

        if(!isset($sidebar_options[$add_to_sidebar])){
            $sidebar_options[$add_to_sidebar] = array('_multiwidget'=>1);
        }
        
        $widget = get_option('widget_'.$widget_name);
        
        if( ! is_array($widget ) ) {
            $widget = array();
        }
        
        $count = count($widget) + 1;
        
        // add first widget to sidebar:
        $sidebar_options[$add_to_sidebar][] = $widget_name.'-'.$count;
        
        $widget[$count] = $settings;
        
        update_option('sidebars_widgets',$sidebar_options);
        update_option('widget_'.$widget_name,$widget);
    }
    
    
    function apollo_update_default_template( $add_widget = 1 ) {
        
        // Update default layout for header, footer, siteinfo
        $config = get_option('apollo_optionsframework');
        
        if ( ! $config['id'] ) {
            $config['id'] = 'apollo';
        }

        $options = get_option( $config['id'] );
        
        $options['apollo_header'] = render_php_to_string( APOLLO_THEME_HEADER );
        $options['apollo_footer'] = render_php_to_string( APOLLO_THEME_FOOTER );
        $options['apollo_site_info'] = render_php_to_string( APOLLO_THEME_SITE_INFO );
        $options['apollo_activate_spotlight'] = APOLLO_THEME_ACTIVATE_SPOTLIGHT;     
        $options['apollo_primary_color'] = APOLLO_THEME_PRIMARY_COLOR;
        $options['apollo_activate_override_css'] = APOLLO_THEME_ACTIVATE_OVERRIDE_CSS;
        
        update_option($config['id'], $options);
        
        // Add widgets
        if ( $add_widget ) {
            $this->add_widgets();
        }
       
        // Update primary color
        Options_Framework_Admin::update_primary_color_css( $options );
    }
}

$default_template_class = new apollo_default_template();
$default_template_class->init();
